package org.groupg.project;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;



public class DisruptorExample2 {
    public static void main(String[] args) throws InterruptedException {
        // 定义 Ring Buffer 大小
        int bufferSize = 1024;

        // 创建 Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<>(
                new LongEventFactory(), bufferSize, DaemonThreadFactory.INSTANCE);

        // 注册多个事件处理器
        disruptor.handleEventsWith(new LongEventHandler1(), new LongEventHandler2());

        // 启动 Disruptor
        disruptor.start();

        // 获取 Ring Buffer
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        // 发布事件
        for (long i = 0; i < 10; i++) {
            long sequence = ringBuffer.next();
            try {
                LongEvent event = ringBuffer.get(sequence);
                event.set(i);
            } finally {
                ringBuffer.publish(sequence);
            }
        }

        // 关闭 Disruptor
        disruptor.shutdown();
    }
}